package jwei.apps.zappos;

import java.util.List;

import jwei.apps.helpers.ConnectionManager;
import jwei.apps.helpers.GetMethods;
import jwei.apps.json.JsonToPurchasedStyle;
import jwei.apps.objects.APIConstants;
import jwei.apps.objects.PurchasedStyle;

import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class ZapposGetLatest {

    private static DefaultHttpClient httpClient = ConnectionManager.getClient();

    public static List<PurchasedStyle> getLatestPurchases() {
        String url = APIConstants.STATISTICS;
        String response = GetMethods.doGetWithResponse(url, httpClient);
        if (!response.equals("")) {
            try {
                JSONObject jsonResponse = new JSONObject(response);
                JSONArray results = jsonResponse.getJSONArray("results");
                List<PurchasedStyle> ret = JsonToPurchasedStyle.convertToPurchasedStyles(results);
                return ret;
            } catch (JSONException e) {
                e.printStackTrace();
                return null;
            }
        } else {
            System.out.println(ZapposGetProduct.class.getCanonicalName() + ": Did not retrieve response");
            return null;
        }
    }

}
